package com.zhiyou.app.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.StrUtil;
import com.aliyuncs.exceptions.ClientException;
import com.zhiyou.app.common.constant.UserHandler;
import com.zhiyou.app.common.vo.resp.Response;
import com.zhiyou.app.config.oss.OssUtil;
import com.zhiyou.app.pojo.vo.ImproveUserProfileVo;
import com.zhiyou.app.pojo.vo.UserInfoVo;
import com.zhiyou.app.pojo.vo.UserLoginRequestVo;
import com.zhiyou.app.pojo.vo.UserLoginResponseVo;
import com.zhiyou.app.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jonathan.z
 * @since 2025-07-03
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Resource
    private IUserService userService;


    @SaIgnore
    @ApiOperation(value = "小程序用户登录")
    @PostMapping("/maLogin")
    public Response<UserLoginResponseVo> doLogin(@RequestBody UserLoginRequestVo userLoginRequestVo) {
        return Response.successWithData(userService.userLogin(userLoginRequestVo));
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/getProfile")
    public Response<UserInfoVo> getProfile(@ApiParam("用户ID，非必传，为空代表查询当前用户") @RequestParam(required = false) String userId) {
        Integer userIdInt = null;
        if (StrUtil.isEmpty(userId)) {
            userIdInt = UserHandler.getLoginUser().getUserId();
        }
        return Response.successWithData(userService.getProfile(userIdInt));
    }

    @ApiOperation(value = "上传头像")
    @PostMapping("/uploadPhoto")
    public Response<String> uploadPhoto(MultipartFile file) {
        return Response.successWithData(userService.uploadPhoto(file));
    }

    @ApiOperation(value = "完善用户信息")
    @PostMapping("/improveUserProfile")
    public Response<UserInfoVo> improveUserProfile(@RequestBody ImproveUserProfileVo improveUserProfileVo) {
        return Response.successWithData(userService.improveUserProfile(improveUserProfileVo));
    }

    @ApiOperation(value = "绑定手机号")
    @PostMapping("/bindPhone")
    public Response<UserInfoVo> bindPhone(@RequestBody String code) {
        return Response.successWithData(userService.bindPhone(code));
    }

    @ApiOperation(value = "生成分享码")
    @PostMapping("/createInvitationCode")
    public Response<String> createInvitationCode() {
        return Response.successWithData(userService.createInvitationCode());
    }

}
